import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jicaimao_flutter/common/constants.dart';
import 'package:jicaimao_flutter/utils/api_address.dart';

import 'package:jicaimao_flutter/utils/http_utils.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:jicaimao_flutter/entity/product_entity.dart';
import 'package:jicaimao_flutter/views/produt/product_detail_page.dart';
import 'package:jicaimao_flutter/entity/adbean.dart';

class HomePage extends StatefulWidget {
  final mProducts = <ProductBean>[
    ProductBean(8, 90, 98),
    ProductBean(10, 180, 246),
    ProductBean(15, 356, 750),
  ];

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> bannerListData = const <String>[
    _BannerString1,
    _BannerString2,
    _BannerString3
  ];
  static const String _BannerString1 =
      "http://api.jicaimao.cn/wallet_upload/banner/1530756282774_872.jpg";
  static const String _BannerString2 =
      "http://api.jicaimao.cn/wallet_upload/banner/1530756289426_464.jpg";
  static const String _BannerString3 =
      "http://api.jicaimao.cn/wallet_upload/banner/1530756295562_186.jpg";

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text(
              '集财猫',
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.w100,
                fontSize: 16.0,
              ),
            ),
            centerTitle: true,
            backgroundColor: Colors.white),
        body: SingleChildScrollView(
          child: Container(
            color: Color(0xFFE4E4E4),
            child: Column(
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 150.0,
                  padding: new EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
                  color: Colors.black12,
                  child: Swiper(
                    itemBuilder: _swiperBuilder,
                    itemCount: bannerListData.length,
                    pagination: SwiperPagination(
                        builder: DotSwiperPaginationBuilder(
                      color: Colors.black54,
                      activeColor: Colors.white,
                    )),
                    scrollDirection: Axis.horizontal,
                    autoplay: true,
                    viewportFraction: 0.8,
                    scale: 0.9,
                    onTap: (index) => print('点击了第$index个'),
                  ),
                ),
                new Image.asset(
                  'images/icon_conduct.png',
                  height: 60.0,
                  width: 600.0,
                  fit: BoxFit.fitWidth,
                ),
                new Container(
                  color: const Color(0xFFE4E4E4),
                  width: 600.0,
                  height: 6.0,
                ),
                new Container(
                  width: MediaQuery.of(context).size.width,
                  height: 300.0,
                  child: CustomScrollView(
                    slivers: <Widget>[
                      SliverList(
                        delegate: SliverChildListDelegate(
                            widget.mProducts.map((ProductBean product) {
                          return ProductItem(
                            product: product,
                          );
                        }).toList()),
                      ),
                    ],
                  ),
                ),
                new Image.asset(
                  'images/icon_banner_default.png',
                  width: 600.0,
                  fit: BoxFit.fitWidth,
                )
              ],
            ),
          ),
        ));
  }

  Widget _swiperBuilder(BuildContext context, int index) => (Image.network(
        bannerListData[index],
        fit: BoxFit.fill,
      ));

  /// 获取首页banner
  void _loadData() async {
    String url = NetAddress.homeBanner;
    var respone = await HttpUtil().post(url);
    List<String> thisBan = <String>[];

    adbean bean = adbean.fromJson(respone);
    if (bean.code == "200") {
      print('****************************************${bean.result.length}');
      for (int i = 0; i < bean.result.length; i++) {
        print(
            '****************************************${bean.result[i].pictureAddress}');
        thisBan.add(bean.result[i].pictureAddress);
      }
    }
    print('=========22222===========${thisBan.length}');
    setState(() {
//        bannerListData = thisBan;
//         print('====================${bannerListData.length}');
    });
  }
}

class ProductItem extends StatelessWidget {
  ProductItem({this.product, this.onTabCallback});

  final ProductBean product;
  final ValueChanged<void> onTabCallback;

  Future<Null> _handleTap(ProductBean product) async {
    await android_platForm
        .invokeMethod('startProductActivity', <String, dynamic>{
      'reward': product.reward.toString(),
      'regularProduct': product.regularProduct.toString(),
      'welfare': product.welfare.toString(),
    });
    print('_handleTap');
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 6.0),
      child: GestureDetector(
        onTap: () {
          _handleTap(product);
        },
        child: Container(
          decoration: BoxDecoration(color: Colors.white),
          height: 72.0,
          child: Row(
            children: <Widget>[
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.reward.toString(),
                        style: TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(text: '%', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '奖励',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.regularProduct.toString(),
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 18.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(text: '天', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '定期产品',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.welfare.toString(),
                        style: TextStyle(
                            color: Colors.red,
                            fontSize: 18.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(
                              text: '(BSC)', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '福利',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
